package com.dfrz.planeclub.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dfrz.planeclub.mapper.DispatcherMapper;
import com.dfrz.planeclub.pojo.PlaneTime;
import com.dfrz.planeclub.service.DispatcherService;
import com.dfrz.planeclub.utill.BaseResult;
import com.dfrz.planeclub.utill.ResultCodeEnum;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Description:
 * ClassName:DispatcherController
 * Package:com.dfrz.planeclub.controller
 *
 * @Author DempseY
 * @Date 2024/4/11 15:13
 * @Version 1.0
 */
@RestController
@CrossOrigin(origins = "*")
public class DispatcherController {

    @Resource
    DispatcherService dispatcherService;


/*
    @RequestMapping("/test")
    @ResponseBody
    public Object getWeather(){
        String key = "9e273f8b9cf6e52374a97aec79c87de9";
        // 城市名
        String city = "110101";
        // 构建请求URL
        String url = "https://restapi.amap.com/v3/weather/weatherInfo?key=" + key + "&city=" + city ;
        System.out.println(url);

        // 创建HttpClient对象
        HttpClient httpClient = HttpClients.createDefault();

        // 创建HttpGet对象
        HttpGet httpGet = new HttpGet(url);
        JSONObject jsonObject = new JSONObject();
        try {
            // 发送请求并获取响应
            HttpResponse response = httpClient.execute(httpGet);

            // 从响应对象中提取字符串
            String result = EntityUtils.toString(response.getEntity(), "UTF-8");

            jsonObject = JSON.parseObject(result);
            // 打印结果
            System.out.println(jsonObject.get("lives"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject.get("lives");
    }
 */

    @GetMapping("/getPlaneTime")
    public BaseResult getPlaneTime(){
        System.out.println("查询所有飞机时间表");
        BaseResult baseResult = null;
        List<PlaneTime> planeTimeList = dispatcherService.planeTimeList();
        Map<String,Object> map = new HashMap<>();
        map.put("arr",planeTimeList);
        baseResult = BaseResult.setResult(ResultCodeEnum.SUCCESS,map);
        System.out.println(baseResult);
        return baseResult;
    }

}
